<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="头像URL" prop="profilePicture">
      <el-input v-model="dataForm.profilePicture" placeholder="头像URL"></el-input>
    </el-form-item>
    <el-form-item label="手机号码" prop="phoneNumber">
      <el-input v-model="dataForm.phoneNumber" placeholder="手机号码"></el-input>
    </el-form-item>
    <el-form-item label="电子邮件" prop="emailAddress">
      <el-input v-model="dataForm.emailAddress" placeholder="电子邮件"></el-input>
    </el-form-item>
    <el-form-item label="生日" prop="birthday">
      <el-input v-model="dataForm.birthday" placeholder="生日"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="registrationDate">
      <el-input v-model="dataForm.registrationDate" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="记录最近一次的登录时间" prop="lastLoginTime">
      <el-input v-model="dataForm.lastLoginTime" placeholder="记录最近一次的登录时间"></el-input>
    </el-form-item>
    <el-form-item label="用户角色（派送员0、普通用户1、商家2、管理员3）" prop="userRole">
      <el-input v-model="dataForm.userRole" placeholder="用户角色（派送员0、普通用户1、商家2、管理员3）"></el-input>
    </el-form-item>
    <el-form-item label="微信用户唯一标识" prop="openId">
      <el-input v-model="dataForm.openId" placeholder="微信用户唯一标识"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          userName: '',
          password: '',
          profilePicture: '',
          phoneNumber: '',
          emailAddress: '',
          birthday: '',
          registrationDate: '',
          lastLoginTime: '',
          userRole: '',
          openId: ''
        },
        dataRule: {
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          profilePicture: [
            { required: true, message: '头像URL不能为空', trigger: 'blur' }
          ],
          phoneNumber: [
            { required: true, message: '手机号码不能为空', trigger: 'blur' }
          ],
          emailAddress: [
            { required: true, message: '电子邮件不能为空', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '生日不能为空', trigger: 'blur' }
          ],
          registrationDate: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          lastLoginTime: [
            { required: true, message: '记录最近一次的登录时间不能为空', trigger: 'blur' }
          ],
          userRole: [
            { required: true, message: '用户角色（派送员0、普通用户1、商家2、管理员3）不能为空', trigger: 'blur' }
          ],
          openId: [
            { required: true, message: '微信用户唯一标识不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.user.userName
                this.dataForm.password = data.user.password
                this.dataForm.profilePicture = data.user.profilePicture
                this.dataForm.phoneNumber = data.user.phoneNumber
                this.dataForm.emailAddress = data.user.emailAddress
                this.dataForm.birthday = data.user.birthday
                this.dataForm.registrationDate = data.user.registrationDate
                this.dataForm.lastLoginTime = data.user.lastLoginTime
                this.dataForm.userRole = data.user.userRole
                this.dataForm.openId = data.user.openId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'userName': this.dataForm.userName,
                'password': this.dataForm.password,
                'profilePicture': this.dataForm.profilePicture,
                'phoneNumber': this.dataForm.phoneNumber,
                'emailAddress': this.dataForm.emailAddress,
                'birthday': this.dataForm.birthday,
                'registrationDate': this.dataForm.registrationDate,
                'lastLoginTime': this.dataForm.lastLoginTime,
                'userRole': this.dataForm.userRole,
                'openId': this.dataForm.openId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
